
#include "stdafx.h"
#include "JPGMutex.h"
HANDLE	g_hMutex = NULL;
/*----------------------------------------------------------------------------
*Function: CreateJPGmutex
*Implementation Notes: Create Mutex handle 
-----------------------------------------------------------------------------*/
HANDLE CreateJPGmutex(void)
{

	g_hMutex = CreateMutex( NULL,                       // default security attributes
		FALSE,                      // initially not owned
		NULL);                      // unnamed mutex

	return g_hMutex;
}

/*----------------------------------------------------------------------------
*Function: LockJPGMutex
*Implementation Notes: lock mutex 
-----------------------------------------------------------------------------*/
DWORD LockJPGMutex(void)
{
	DWORD				Status;


	Status = WaitForSingleObject(g_hMutex, INFINITE);
	if(Status == WAIT_OBJECT_0) 
	{
		Status = TRUE;
	} 
	else 
	{
		Status = GetLastError();
		RETAILMSG(1,(TEXT("DD::MUTEX LOCK ERROR : %d\r\n"),Status));
		Status = FALSE;
	}


	return Status;
}

/*----------------------------------------------------------------------------
*Function: UnlockJPGMutex
*Implementation Notes: unlock mutex
-----------------------------------------------------------------------------*/
DWORD UnlockJPGMutex(void)
{
	DWORD				Status;

	Status = ReleaseMutex(g_hMutex);
	if(Status != TRUE) 
	{
		Status = GetLastError();
		RETAILMSG(1,(TEXT("DD::MUTEX UNLOCK ERROR : %d\r\n"),Status));

	}
	//RETAILMSG(1,(TEXT("MUTEX is : 0x%x\r\n"),g_hMutex));

	return Status;
}

/*----------------------------------------------------------------------------
*Function: DeleteJPGMutex
*Implementation Notes: delete mutex handle 
-----------------------------------------------------------------------------*/
void DeleteJPGMutex(void)
{
	if (g_hMutex == NULL)
		return;

	CloseHandle(g_hMutex);
}

